package com.shy.springframework.test.bean;

import com.shy.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yzpang
 * @desc:
 * @date: 2023/2/9 14:34
 */
public class ProxyBeanFactory implements FactoryBean<IUserDao> {
    @Override
    public IUserDao getObject() throws Exception {
        InvocationHandler handler = (proxy, method, args)->{
            Map<String, String> map = new HashMap<>();
            map.put("10001", "yzpang");
            map.put("10002", "lixiaolong");
            map.put("10003", "wangzhixiang");
            map.put("10004", "zhangyishan");

            return "你被代理了 method: " + method.getName() + " : " + map.get(args[0].toString());
        };
        return (IUserDao) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{IUserDao.class}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
